//
// Copyright 2008-2011 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace YaTools.Yaml
{
    partial class Dump
    {
        public abstract partial class Decorator
        {
            class Json : Decorator
            {
                private const int CheckEveryN = 100;

                private int _checkDepth = CheckEveryN;
                private readonly Stack<object> _stack = new Stack<object>(32);

                public override bool PresentNode(Dump dump, object value)
                {
                    if (_stack.Count == _checkDepth)
                    {
                        _checkDepth += CheckEveryN;

                        if (_stack.Contains(value))
                        {
                            throw new ArgumentException("Json serializtion doesn't support non-tree structured data");
                        }
                    }

                    _stack.Push(value);

                    return true; // Json doesn't support tags or anchors so we have to serialize everything
                }

                public override void Preprocess(Dump dump, object value)
                {
                    // Noop for Json
                }

                public override string HandleForTag(string tag)
                {
                    return string.Empty;
                }
            }
        }
    }
}